
package com.tools.cleanmaster.utils;

import android.annotation.SuppressLint;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.zip.ZipInputStream;

public class StringCompress {

    public static ByteArrayOutputStream decompress(ByteArrayInputStream paramArrayOfByte) {
        if (paramArrayOfByte == null)
            return null;
        ZipInputStream zipInputStream = null;
        ByteArrayOutputStream bao = null;
        try {
            bao = new ByteArrayOutputStream();
            zipInputStream = new ZipInputStream(paramArrayOfByte);
            zipInputStream.getNextEntry();
            byte[] arrayOfByte = new byte[1024];
            for (int i = -1; (i = zipInputStream.read(arrayOfByte)) != -1;) {
                bao.write(arrayOfByte, 0, i);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (zipInputStream != null) {
                try {
                    zipInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return bao;
    }

    @SuppressLint("UseSparseArrays")
    public static HashMap<String, String> deCodeString(byte[] datas, char[] hexchar) {
        String enString = new String(datas);
        char[] charArray = enString.toCharArray();
        HashMap<String, String> paths = new HashMap<String, String>();
        for (int i = 0; i < charArray.length; i++) {
            charArray[i] = (char) (charArray[i] ^ hexchar[i % hexchar.length]);
        }
        String decodeStr = new String(charArray);
        String[] pathArray = decodeStr.split("\n");
        for (String path : pathArray) {
            String[] split = path.split(":");
            if(split.length>1){
                paths.put(split[0], split[1]);
            }
        }
        if (paths.size() < 1)
            return null;
        return paths;
    }

    /**
     * @param datas 保证非null
     * @param key 需保证非null
     */
    public static void enCodeStringByXOr(char[] datas, char[] key) {
        if (datas == null || datas.length == 0) {
            return;
        }
        for (int i = 0; i < datas.length; i++) {
            datas[i] = (char) (datas[i] ^ key[i % key.length]);
        }
    }

    public static char[] enCodeStringByXOr(String datas, String key) {
        if (datas == null || datas.length() == 0 || key == null || key.length() == 0) {
            return null;
        }
        final int len = datas.length();
        final int keyLen = key.length();
        char[] encodedData = new char[len];
        for (int i = 0; i < len; i++) {
            encodedData[i] = (char) (datas.charAt(i) ^ key.charAt(i % keyLen));
        }
        return encodedData;
    }

    public static String enCodeString(String datas, String key) {
        char c[] = enCodeStringByXOr(datas, key);
        if (c == null || c.length == 0) {
            return "";
        }
        return String.valueOf(c);
    }
}
